/*
 * test.c
 *
 *  Created on: Mar 10, 2023
 *      Author: lzc
 */

/* Change this if the SERVER_NAME environment variable does not report
	the true name of your web server. */
#if 1
#define SERVER_NAME cgiServerName
#endif
#if 0
#define SERVER_NAME "www.boutell.com"
#endif

/* You may need to change this, particularly under Windows;
	it is a reasonable guess as to an acceptable place to
	store a saved environment in order to test that feature.
	If that feature is not important to you, you needn't
	concern yourself with this. */

#ifdef WIN32
#define SAVED_ENVIRONMENT "c:\\cgicsave.env"
#else
#define SAVED_ENVIRONMENT "/tmp/cgicsave.env"
#endif /* WIN32 */

#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>
#include "encodeURL.h"
#include "decodeURL.h"

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
#include "config_ini.h"
#include "log.h"


int cgiMain() {

	char obj[1024] = {0};
	char curl_cmd[1024];
	char curl_cmd_en[1024];

    cgiHeaderContentType("text/html; charset=UTF-8");

    if(cgiContentLength>0)
	{

		unsigned int len = strlen(cgiQueryString);
		int resultSize = URLDecode(cgiQueryString, len, obj, 1024);

		LogWrite("INFO",obj);

		cJSON *jsonroot = cJSON_Parse(obj);
		cJSON *value = cJSON_GetObjectItem(jsonroot, "cmd");
		if(value != NULL)
		{
			if(strcmp(value->valuestring,"get") == 0)
			{
				value = cJSON_GetObjectItem(jsonroot, "key");
				if(strcmp(value->valuestring,"device id") == 0)
				{
					cJSON* pRoot = cJSON_CreateObject();
					cJSON* pArray = cJSON_CreateArray();
					cJSON_AddStringToObject(pRoot, "cmd", "get");
					//cJSON_AddNumberToObject(pRoot, "num", 123456);
					cJSON_AddStringToObject(pRoot, "key", "device_id");
					cJSON_AddStringToObject(pRoot, "code", "ok");
					cJSON_AddItemToObject(pRoot, "value", pArray);
					cJSON* pItem = cJSON_CreateObject();
					cJSON_AddStringToObject(pItem, "key", "id");
					cJSON_AddStringToObject(pItem, "value", "hL8clQjkd43ZxDe2");
					cJSON_AddItemToArray(pArray, pItem);

					char* szJSON = cJSON_Print(pRoot);
					fprintf(cgiOut,"%s\n",szJSON );
					cJSON_Delete(pRoot);
				}
				if(strcmp(value->valuestring,"server ip") == 0)
				{

				    char remote_ip[32] = {0};
				    char remote_port[32] = {0};

				    configFileStrGet("/boa/my.ini","remote_ip",remote_ip,32);
				    configFileStrGet("/boa/my.ini","remote_port",remote_port,32);

					cJSON* pRoot = cJSON_CreateObject();
					cJSON* pArray = cJSON_CreateArray();
					cJSON_AddStringToObject(pRoot, "cmd", "get");
					//cJSON_AddNumberToObject(pRoot, "num", 123456);
					cJSON_AddStringToObject(pRoot, "key", "server ip");
					cJSON_AddStringToObject(pRoot, "code", "ok");
					cJSON_AddItemToObject(pRoot, "value", pArray);

					cJSON* pItem = cJSON_CreateObject();
					cJSON_AddStringToObject(pItem, "key", "ip");
					cJSON_AddStringToObject(pItem, "value", remote_ip);
					cJSON_AddItemToArray(pArray, pItem);

					pItem = cJSON_CreateObject();
					cJSON_AddStringToObject(pItem, "key", "port");
					cJSON_AddStringToObject(pItem, "value", remote_port);
					cJSON_AddItemToArray(pArray, pItem);
					char* szJSON = cJSON_Print(pRoot);
					fprintf(cgiOut,"%s\n",szJSON );
					cJSON_Delete(pRoot);
				}

				if(strcmp(value->valuestring,"test image") == 0)
				{

					char local_ip[32] = {0};
				    char remote_ip[32] = {0};
				    char remote_port[32] = {0};

				    configFileStrGet("/boa/my.ini","local_ip",local_ip,32);
				    configFileStrGet("/boa/my.ini","remote_ip",remote_ip,32);
				    configFileStrGet("/boa/my.ini","remote_port",remote_port,32);


					sprintf(curl_cmd,"/usr/bin/curl \"http://%s:%s/newimage?type=info&imgid=13&groupid=0&url=%s\"",remote_ip,remote_port,local_ip);
					//fprintf(cgiOut,"%s\n",curl_cmd);
					system(curl_cmd);

					sprintf(curl_cmd,"/usr/bin/curl \"http://%s:%s/newimage?type=small&imgid=13&groupid=0&url=%s/jpeg_0_64.jpg&listurl=%s/header_0_64.txt\"",remote_ip,remote_port,local_ip,local_ip);
					//fprintf(cgiOut,"%s\n",curl_cmd);
					system(curl_cmd);

					sprintf(curl_cmd,"/usr/bin/curl \"http://%s:%s/newimage?type=big&imgid=13&groupid=0&url=%s/jpeg_0_128.jpg&listurl=%s/header_0_128.txt\"",remote_ip,remote_port,local_ip,local_ip);
					//fprintf(cgiOut,"%s\n",curl_cmd);
					system(curl_cmd);
				}
			}
			if(strcmp(value->valuestring,"set") == 0)
			{
				value = cJSON_GetObjectItem(jsonroot, "key");

				if(strcmp(value->valuestring,"server ip") == 0)
				{

					cJSON *arrayItem = cJSON_GetObjectItem(jsonroot,"params");
					cJSON *object = cJSON_GetArrayItem(arrayItem,0);
					value = cJSON_GetObjectItem(object,"key");
					//fprintf(cgiOut,"1111 = %s\n",value->valuestring);
					value = cJSON_GetObjectItem(object,"value");
					//fprintf(cgiOut,"1111 = %s\n",value->valuestring);
					configFileStrSet("/boa/my.ini","remote_ip",value->valuestring);

					object = cJSON_GetArrayItem(arrayItem,1);
					value = cJSON_GetObjectItem(object,"key");
					//fprintf(cgiOut,"1111 = %s\n",value->valuestring);
					value = cJSON_GetObjectItem(object,"value");
					//fprintf(cgiOut,"222 = %s\n",value->valuestring);
					configFileStrSet("/boa/my.ini","remote_port",value->valuestring);


					cJSON* pRoot = cJSON_CreateObject();
					cJSON_AddStringToObject(pRoot, "cmd", "set");
					cJSON_AddStringToObject(pRoot, "key", "server ip");
					cJSON_AddStringToObject(pRoot, "code", "ok");

					char* szJSON = cJSON_Print(pRoot);
					fprintf(cgiOut,"%s\n",szJSON );
					cJSON_Delete(pRoot);





				}

				if(strcmp(value->valuestring,"reboot") == 0)
				{
					system("/sbin/reboot &");
					//system("echo \"111111\" | sudo /bin/reboot &");

				}

			}

		}
		cJSON_Delete(jsonroot);
	}

	return 0;
}

